package com.piggy.logi.sys.controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Console;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.piggy.common.core.utils.ip.AddressUtils;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.satoken.annotation.Inner;
import com.piggy.common.core.domain.R;
import org.springframework.web.bind.annotation.*;
import com.piggy.common.core.utils.ServletUtils;
import com.piggy.common.core.utils.ip.IpUtils;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.enums.BusinessType;

import com.piggy.logi.sys.api.domain.LogiSysLogininfor;
import com.piggy.logi.sys.service.ILogiSysLogininforService;

/**
 * 系统访问记录
 *
 * @author shark
 */
@RestController
@RequestMapping("/logi/logininfor")
public class LogiSysLogininforController extends BaseController {
    @Resource
    private ILogiSysLogininforService logininforService;

    //@SaCheckPermission("system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(LogiSysLogininfor logininfor) {
        startPage();
        List<LogiSysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @LogiLog(title = "登录日志", businessType = BusinessType.EXPORT)
    //@SaCheckPermission("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, LogiSysLogininfor logininfor) throws IOException {
        List<LogiSysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<LogiSysLogininfor> util = new ExcelUtil<LogiSysLogininfor>(LogiSysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    //@SaCheckPermission("system:logininfor:remove")
    @LogiLog(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R remove(@PathVariable Long[] infoIds) {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

    //@SaCheckPermission("system:logininfor:remove")
    @LogiLog(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public R clean() {
        logininforService.cleanLogininfor();
        return R.ok();
    }

    @Inner
    @PostMapping
    public R<Boolean> add(@RequestBody LogiSysLogininfor logiSysLogininfor) {
//        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());

        // 封装对象
//        SysLogininfor logininfor = new SysLogininfor();
//        logininfor.setUserName(username);
//        logininfor.setIpaddr(ip);
//        logininfor.setMsg(message);
//        // 日志状态
//        if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status)) {
//            logininfor.setStatus("0");
//        } else if (Constants.LOGIN_FAIL.equals(status)) {
//            logininfor.setStatus("1");
//        }
        UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        // 获取客户端操作系统
        String os = userAgent.getOs().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        String address = AddressUtils.getRealAddressByIP(ip);
        logiSysLogininfor.setLoginLocation(address);
        logiSysLogininfor.setBrowser(browser);
        logiSysLogininfor.setOs(os);

        StringBuilder s = new StringBuilder();
        s.append(getBlock(ip));
        s.append(address);
        s.append(getBlock(logiSysLogininfor.getUserName()));
        s.append(getBlock(logiSysLogininfor.getMsg()));
        s.append(getBlock(logiSysLogininfor.getStatus()));
        // 打印信息到日志
        Console.log(s.toString(), logiSysLogininfor.getMsg());

        return R.ok(logininforService.insertLogininfor(logiSysLogininfor));
    }


    public static String getBlock(Object msg)
    {
        if (msg == null)
        {
            msg = "";
        }
        return "[" + msg.toString() + "]";
    }
}
